module.exports = async (page, asin) => {
  await page.goto("https://www.amazon.com/dp/" + asin);
  await page.waitForSelector("body");

  return await page.evaluate((asin) => {
    /* Get product title */
    let title =
      (document.body.querySelector("#productTitle") &&
        document.body.querySelector("#productTitle").innerText) ||
      "";
    /* Get product image */
    let image =
      document.querySelector(".imgTagWrapper .a-dynamic-image").currentSrc ||
      "";
    /* Get review count */
    let reviewCountDom = document.body.querySelector("#acrCustomerReviewText");
    let reviewCount = (reviewCountDom && reviewCountDom.innerText) || "";
    let formattedReviewCount = reviewCount.replace(/[^0-9]/g, "").trim();

    /* Get and format rating */
    let ratingElement =
      document.body.querySelector(".a-icon.a-icon-star") || "";
    let integer = "";
    let parsedRating = 10;
    if (ratingElement) {
      integer =
        (ratingElement.getAttribute("class") &&
          ratingElement
            .getAttribute("class")
            .replace(/[^0-9]/g, "")
            .trim()) ||
        "";
      parsedRating = parseInt(integer) / 10;
    }

    /* Get availability */
    let availabilityDom = document.body.querySelector("#availability");
    let availability = (availabilityDom && availabilityDom.innerText) || "";
    let formattedAvailability = availability.replace(/[^0-9]/g, "").trim();

    /* Get list price */
    let listPriceDom =
      document.body.querySelector(".basisPrice .a-offscreen") || "";

    let listPrice =
      listPriceDom && listPriceDom.innerText.replace(/[^0-9.]/g, "").trim();

    /* Get price */
    let priceDom = document.body.querySelector(".a-price .a-offscreen") || "";
    let price = priceDom && priceDom.innerText.replace(/[^0-9.]/g, "").trim();

    /* Get product description */
    // let description = document.body.querySelector(
    //   "#renewedProgramDescriptionAtf"
    // ).innerText;

    /* Get product features */
    let features = document.body.querySelectorAll("#feature-bullets ul li");
    let formattedFeatures = [];

    features.forEach((feature) => {
      formattedFeatures.push(feature.innerText);
    });

    /* Get comparable items */
    // let comparableItems = document.body.querySelectorAll(
    //   "#HLCXComparisonTable .comparison_table_image_row .a-link-normal"
    // );
    // formattedComparableItems = [];

    // comparableItems.forEach((item) => {
    //   formattedComparableItems.push(
    //     "https://amazon.com" + item.getAttribute("href")
    //   );
    // });

    let itemModelNumber = "";
    let manufacturerPartNumber = "";
    document.body
      .querySelectorAll("#productDetails_techSpec_section_1 tr")
      .forEach((v) => {
        if (v.innerText.includes("Item model number")) {
          itemModelNumber = v.innerText.split("\t")[1].trim();
        }
        if (v.innerText.includes("Manufacturer Part Number")) {
          manufacturerPartNumber = v.innerText.split("\t")[1].trim();
        }
      });

    return {
      asin,
      title: title,
      image,
      rating: parsedRating,
      reviewCount: formattedReviewCount,
      listPrice: listPrice,
      price: price,
      availability: formattedAvailability,
      // description: description,
      features: formattedFeatures,
      itemModelNumber,
      manufacturerPartNumber,
      // comparableItems: formattedComparableItems,
    };
  }, asin);
};
